package com.mo.axion.handler.action;

import cn.hutool.core.collection.CollUtil;
import com.mo.axion.common.domain.TaskInfo;
import com.mo.axion.common.enums.DeduplicationType;
import com.mo.axion.common.enums.EnumUtil;
import com.mo.axion.common.pipeline.BusinessProcess;
import com.mo.axion.common.pipeline.ProcessContext;
import com.mo.axion.handler.deduplication.DeduplicationHolder;
import com.mo.axion.handler.deduplication.DeduplicationParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author Mo
 * @createTime 2024/2/20 16:36
 * @description 去重步骤
 * 1. 根据相同内容N分钟去重（SlideWindowLimitService）
 * 2. 相同的渠道一天内频次去重（SimpleLimitService）
 */
@Service
public class DeduplicationAction implements BusinessProcess<TaskInfo> {
    public static final String DEDUPLICATION_RULE_KEY = "deduplicationRule";

    @Autowired
    private DeduplicationHolder deduplicationHolder;

    /**
     * 真正处理逻辑
     *
     * @param context
     */
    @Override
    public void process(ProcessContext<TaskInfo> context) {
        TaskInfo taskInfo = context.getProcessModel();
        String deduplicationConfig = "{\"deduplication_10\":{\"num\":1,\"time\":300},\"deduplication_20\":{\"num\":5}}";
        List<Integer> deduplicationList = EnumUtil.getCodeList(DeduplicationType.class);
        for (Integer deduplicationType : deduplicationList) {
            DeduplicationParam deduplicationParam = deduplicationHolder.selectBuilder(deduplicationType).build(deduplicationConfig, taskInfo);
            if(Objects.nonNull(deduplicationParam)) {
                deduplicationHolder.selectService(deduplicationType).deduplication(deduplicationParam);
            }
            if (CollUtil.isEmpty(taskInfo.getReceiver())) {
                context.setIsNeedBreak(true);
            }
        }
    }
}
